<?php
//公共
namespace Home\Controller;
use Think\Controller;
class CommonController extends Controller {

	function _initialize(){
        defined('TMPL_PATH') or define("TMPL_PATH", C("SP_TMPL_PATH"));

        $the_host = $_SERVER['HTTP_HOST'];//取得当前域名
        $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';//判断地址后面是否有参数
        if($the_host != 'www.58daiyan.com')//把这里的域名换上你想要的
        {
            header('HTTP/1.1 301 Moved Permanently');//发出301头部
            header('Location: http://www.58daiyan.com'.$request_uri);//跳转到你希望的地址格式
        }


        //php判断客户端是否为手机
		$agent = $_SERVER['HTTP_USER_AGENT'];
		if( !preg_match('/m.58daiyan.com/',$_SERVER['HTTP_HOST']) ){
			if( strpos($agent,"NetFront") || strpos($agent,"iPhone") || strpos($agent,"MIDP-2.0") || strpos($agent,"Opera Mini") || strpos($agent,"UCWEB") || strpos($agent,"Android") || strpos($agent,"Windows CE") || strpos($agent,"SymbianOS")|| strpos($agent,"X11")){

				$HTTP_HOST	=	'm.58daiyan.com';
				$url	=	'http://'.$HTTP_HOST.$_SERVER['REQUEST_URI'];
				header("Location:".$url);
			}
		}



        $citys=M('sys_city');

        $getIp=$this->real_ip();
        $city_address= $this->GetIpLookup($getIp);
        $city_address='%'.$city_address.'%';
        $citys_map['city_name'] = array('like',$city_address);
        $rs_d_city=$citys->where($citys_map)->find();

        if(session('site_city')==null||session('site_city_id')==null){
            session('site_city',$rs_d_city['city_name']);
            session('site_city_id',$rs_d_city['city_id']);
        }else{
            if(session('site_city')!=$rs_d_city['city_name']||session('site_city_id')!=$rs_d_city['city_id']){
                if(session('no_qiecity')!=1){
                    $this->assign('rs_d_city',$rs_d_city);
                }

            }
        }



        $rs_citys=$citys->select();
        $city_settlesRes = array();
        foreach ($rs_citys as $k=>$v){

            $city_info['name']=$v['city_name'];
            $city_info['city_id']=$v['city_id'];
            $city_info['spell']=$v['header_code'];
            $city_info['url']=U('index/subsite',array('id'=>$v['city_id']));
            $city_settlesRes[$v['header_code']][] =$city_info;//以这个首字母作为key
        }
        ksort($city_settlesRes);
        $city_settlesRes=json_encode($city_settlesRes);
        $this->assign('city_settlesRes',$city_settlesRes);
        $the_host = $_SERVER['HTTP_HOST'];//取得当前域名
       $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';//判断地址后面是否有参数
       if($the_host == '58daiyan.com')//把这里的域名换上你想要的
       {
           header('HTTP/1.1 301 Moved Permanently');//发出301头部
           header('Location: http://www.58daiyan.com'.$request_uri);//跳转到你希望的地址格式
       }


        $site_options=get_site_options();
        $this->assign($site_options);
      	$get_web_invoice=get_web_invoice();
      	$this->assign($get_web_invoice);
        //长时间登陆判断
    	if(isset($_COOKIE['user_id'])){
			//用cookie给session赋值
			$_SESSION['user_id']=$_COOKIE['user_id'];
			$_SESSION['user_name']=$_COOKIE['user_name'];
		}
		$store=M('store');
		$store_arr=$store
				->join('yt_sys_city on yt_sys_city.city_id=yt_store.city')
				->field('yt_store.city,yt_sys_city.city_name ')
				->where('yt_store.status=1')
				->group('yt_store.city')
				->select();
		if(session('site_city')==null||session('site_city_id')==null){
			if($store_arr){
				session('site_city',$store_arr[0]['city_name']);
				session('site_city_id',$store_arr[0]['city']);
			}else{
				session('site_city','58代言');
			}
		}


		$this->assign('store_arr',$store_arr);
	}

	public function ajax_r($code='0',$data='',$message='') {
		$msg['code']=$code;
		$msg['data']=$data;
		$msg['message']=$message;
	 	$this->ajaxReturn($msg);
	}


    //处理所有没有的方法的处理方法，引导到404页面
    public function _empty() {
        R('Empty/_empty');
    }
    /**
     * 加载模板和页面输出 可以返回输出内容
     * @access public
     * @param string $templateFile 模板文件名
     * @param string $charset 模板输出字符集
     * @param string $contentType 输出类型
     * @param string $content 模板输出内容
     * @return mixed
     */
    public function display($templateFile = '', $charset = '', $contentType = '', $content = '', $prefix = '') {
        parent::display($this->parseTemplate($templateFile), $charset, $contentType,$content,$prefix);
    }

    /**
     * 获取输出页面内容
     * 调用内置的模板引擎fetch方法，
     * @access protected
     * @param string $templateFile 指定要调用的模板文件
     * 默认为空 由系统自动定位模板文件
     * @param string $content 模板输出内容
     * @param string $prefix 模板缓存前缀*
     * @return string
     */
    public function fetch($templateFile='',$content='',$prefix=''){
        $templateFile = empty($content)?$this->parseTemplate($templateFile):'';
        return parent::fetch($templateFile,$content,$prefix);
    }

    /**
     * 自动定位模板文件
     * @access protected
     * @param string $template 模板文件规则
     * @return string
     */
    public function parseTemplate($template='') {

        $tmpl_path=C("SP_TMPL_PATH");
        define("SP_TMPL_PATH", $tmpl_path);
        if($this->theme) { // 指定模板主题
            $theme = $this->theme;
        }else{
            // 获取当前主题名称
            $theme      =    C('SP_DEFAULT_THEME');
            if(C('TMPL_DETECT_THEME')) {// 自动侦测模板主题
                $t = C('VAR_TEMPLATE');
                if (isset($_GET[$t])){
                    $theme = $_GET[$t];
                }elseif(cookie('think_template')){
                    $theme = cookie('think_template');
                }
                if(!file_exists($tmpl_path."/".$theme)){
                    $theme  =   C('SP_DEFAULT_THEME');
                }
                cookie('think_template',$theme,864000);
            }
        }

        $theme_suffix="";

        if(C('MOBILE_TPL_ENABLED') && sp_is_mobile()){//开启手机模板支持

            if (C('LANG_SWITCH_ON',null,false)){
                if(file_exists($tmpl_path."/".$theme."_mobile_".LANG_SET)){//优先级最高
                    $theme_suffix  =  "_mobile_".LANG_SET;
                }elseif (file_exists($tmpl_path."/".$theme."_mobile")){
                    $theme_suffix  =  "_mobile";
                }elseif (file_exists($tmpl_path."/".$theme."_".LANG_SET)){
                    $theme_suffix  =  "_".LANG_SET;
                }
            }else{
                if(file_exists($tmpl_path."/".$theme."_mobile")){
                    $theme_suffix  =  "_mobile";
                }
            }
        }else{
            $lang_suffix="_".LANG_SET;
            if (C('LANG_SWITCH_ON',null,false) && file_exists($tmpl_path."/".$theme.$lang_suffix)){
                $theme_suffix = $lang_suffix;
            }
        }

        $theme=$theme.$theme_suffix;

        C('SP_DEFAULT_THEME',$theme);

        $current_tmpl_path=$tmpl_path.$theme."/";
        // 获取当前主题的模版路径
        define('THEME_PATH', $current_tmpl_path);

        C("TMPL_PARSE_STRING.__TMPL__",__ROOT__."/".$current_tmpl_path);


        C('SP_VIEW_PATH',$tmpl_path);
        C('DEFAULT_THEME',$theme);

        define("SP_CURRENT_THEME", $theme);

        if(is_file($template)) {
            return $template;
        }
        $depr       =   C('TMPL_FILE_DEPR');
        $template   =   str_replace(':', $depr, $template);

        // 获取当前模块
        $module   =  MODULE_NAME;
        if(strpos($template,'@')){ // 跨模块调用模版文件
            list($module,$template)  =   explode('@',$template);
        }

        $module =$module."/";

        // 分析模板文件规则
        if('' == $template) {
            // 如果模板文件名为空 按照默认规则定位
            $template = CONTROLLER_NAME . $depr . ACTION_NAME;
        }elseif(false === strpos($template, '/')){
            $template = CONTROLLER_NAME . $depr . $template;
        }

        $file = sp_add_template_file_suffix($current_tmpl_path.$module.$template);
        $file= str_replace("//",'/',$file);
        if(!file_exists_case($file)) E(L('_TEMPLATE_NOT_EXIST_').':'.$file);
        return $file;
    }

  

}
?>
